package com.csdj.t309.geamtest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csdj.t309.geamtest.common.CacheConstant;
import com.csdj.t309.geamtest.common.WebResult;
import com.csdj.t309.geamtest.controller.MailController;
import com.csdj.t309.geamtest.entity.Jifen;
import com.csdj.t309.geamtest.entity.User;
import com.csdj.t309.geamtest.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.t309.geamtest.util.Md5Utils;
import com.csdj.t309.geamtest.util.RandomUtil;
import com.csdj.t309.geamtest.util.ShiJianUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 田港
 * @since 2022-01-01
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private RedisService redisService;
    @Resource
    private UserMapper userMapper;
    private String yaoqing;
    @Resource
    private IJifenService iJifenService;

    @Override
    public User login(String email, String password) {
        String dePass = Md5Utils.md5(password);
        if (StringUtils.hasText(dePass)) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(User::getEmail, email).eq(User::getPassword, dePass);
            User user = getOne(queryWrapper);
            if (user != null) {
                Integer num1 = new ShiJianUtil().shiJian();
                redisService.setString("login", "计时", num1);
                setToken(user);
                redisService.setObject(CacheConstant.HEADER_UID+user.getId(), user.getId());
                redisService.setString(CacheConstant.TOKEN_PREFIX + user.getId(), user.getToken(), 3600);
                return user;
            }
            return null;
        }
        return null;
    }

    @Override
    public Integer addUser(String email, String password, String inviteCode,Integer id) {
        Integer num1 = new ShiJianUtil().shiJian();
        redisService.setString("yan", "验证", num1);
        Integer sum = 0;
        User user = new User();
        user.setEmail(email);
        user.setPassword(Md5Utils.md5(password));
        user.setAvatar("http://localhost:8082/image/src=http___i0.qhimg.com_dmsmty_350_200__t01d269f7a305bcc96f.jpg&refer=http___i0.qhimg.jpg");
        Date date = new Date();
        user.setCreationDate(date);
        user.setIntegrate(10);
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        List<User> list = userMapper.selectByMap(map1);
        boolean ok = false;
        Integer pid = 0;
        if (inviteCode != null) {
            for (User user1 : list) {
                if (user1.getId().equals(id)) {
                    sum = user1.getIntegrate();
                }
                if (user1.getInviteCode() != null) {
                    if (user1.getInviteCode().equals(inviteCode)) {
                        ok = true;
                        pid = user1.getId();
                    }
                }
            }
            HashMap<String, Object> map = new HashMap<String, Object>();
            //自定义要查询
            map.put("invite_code", inviteCode);
            User user1 = userMapper.selectByMap(map).get(0);
            User upUser = new User();
            upUser.setId(user1.getId());
            upUser.setIntegrate(sum + 20);
            userMapper.updateById(upUser);
            Jifen jifen = new Jifen();
            jifen.setJiUserId(user1.getId());
            jifen.setJiNum("+30");
            jifen.setJiOperateType("邀请好友");
            jifen.setJiOperateDate(date);
            jifen.setJiRemainder(sum + 20);
            iJifenService.addJiFen(jifen);
        }
        if (ok) {
            user.setPid(pid);
        } else {
            user.setPid(0);
        }
        Integer num = userMapper.insert(user);
        if (num > 0) {
            return num;
        }
        return 0;
    }

    @Override
    public boolean emailTesting(String email) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //自定义要查询
        map.put("email", email);
        List<User> userList = userMapper.selectByMap(map);
        if (userList.size() > 0) {
            return false;
        }
        return true;
    }



    @Override
    public String updeteyouqin(Integer id) {
        String suiji = MailController.getCheckCode();
        yaoqing = null;
        User user = new User();
        user.setId(id);
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<User> list = userMapper.selectByMap(map);
        for (User user1 : list) {
            if (user1.getInviteCode() != null) {
                if (user1.getInviteCode().equals(suiji)) {
                    suiji = MailController.getCheckCode();
                }
            }
        }
        for (User user1 : list) {
            if (user1.getId().equals(id)) {
                if (user1.getInviteCode() == null) {
                    yaoqing = suiji;
                    user.setInviteCode(yaoqing);
                } else {
                    yaoqing = user1.getInviteCode();
                }
            }
        }
        user.setInviteCode(yaoqing);
        Integer num = userMapper.updateById(user);
        if (num > 0) {
            return yaoqing;
        }
        return null;
    }

    @Override
    public Integer updateTuPian(User user) {
        Integer num = userMapper.updateById(user);
        if (num > 0) {
            return num;
        }
        return 0;
    }

    @Override
    public Integer updatename(User user) {
        System.out.println(user.toString());
        Integer num = userMapper.updateById(user);
        if (num > 0) {
            return num;
        }
        return 0;
    }

    @Override
    public User seltupian(Integer id) {
        User user1 = userMapper.selectById(id);
        if (user1 != null) {
            return user1;
        }
        return null;
    }

    @Override
    public User selpassword(String email) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //自定义要查询
        map.put("email", email);
        List<User> list = userMapper.selectByMap(map);
        if (list.size()>0) {
            User user = list.get(0);
            return user;
        }
        return null;
    }


    private void setToken(User user) {
        if (user != null) {
            user.setToken(RandomUtil.genToken());
        }
    }


}
